package com.mdinfo.gw.door.service.impl

import com.mdinfo.base.common.bean.CommonResult
import com.mdinfo.base.common.bean.DataGrid
import com.mdinfo.base.common.bean.Ref
import com.mdinfo.base.utils.SystemUtil
import com.mdinfo.base.utils.TimeUtil
import com.mdinfo.gw.door.bean.Door
import com.mdinfo.gw.door.bean.DoorAccess
import com.mdinfo.gw.door.driver.DoorDriver
import com.mdinfo.gw.door.mapper.*
import com.mdinfo.gw.door.param.*
import com.mdinfo.gw.door.service.IDoorService
import com.mdinfo.gw.door.vo.DoorCardListDoorVo
import com.mdinfo.gw.door.vo.DoorListByDoorAccessIdVo
import com.mdinfo.gw.door.vo.DoorListDoorContainChildrenVo
import com.mdinfo.gw.door.vo.DoorListQueryVo
import com.mdinfo.system.bean.SysDept
import com.mdinfo.system.mapper.SysDeptMapper
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

/**
 * Created by Administrator on 2018/1/12.
 */
@Service
@Transactional
class DoorServiceImpl(
        val doorMapper:DoorMapper,
        val deptMapper:SysDeptMapper,
        val doorAccessMapper:DoorAccessMapper,
        var doorCardMapper: DoorCardMapper,
        var doorDriver: DoorDriver,
        var cardTimeBindMapper: DoorCardTimeBindMapper
) : IDoorService{

    override fun listQuery(param: DoorListQueryParam)
            : CommonResult<DataGrid<DoorListQueryVo>> {
        val info = SystemUtil.getInfo()
        val list = doorMapper.listQuery(param.start!!,
                param.num!!,
                param.key,
                param.deptId,
                info.deptIdList)
        val count = doorMapper.countListQuery(param.key!!,
                param.deptId,info.deptIdList)

        val vos:List<DoorListQueryVo> = list.map {
            val vo:DoorListQueryVo = DoorListQueryVo()
            vo.doorId = it.id
            vo.name = it.name
            vo.createTime = TimeUtil.formater(it.createTime)
            vo.deptId = it.deptId
            val dept = deptMapper.selectById(it.deptId)
            vo.deptName = dept?.shortName?:""
            vo.devId = it.devId
            vo.doorAccessId = it.doorAccessId
            val doorAccess = doorAccessMapper.selectById(it.doorAccessId)
            vo.doorAccessName = doorAccess?.name?:""
            vo.lastEvent = it.lastEvent
            vo.accessOrderNum = it.accessOrderNum

            vo.lastEventTime = if(it.lastEventTime==null){
                ""
            }else{
                TimeUtil.formater(it.lastEventTime)
            }
            vo.lat = it.lat
            vo.lng = it.lng
            vo
        }
        val dataGrid = DataGrid<DoorListQueryVo>()
        dataGrid.rows = vos
        dataGrid.total = count
        return Ref.ok(dataGrid)
    }


    /**添加*/
    override fun add(param: DoorAddParam): CommonResult<Void>{

        var door: Door? = doorMapper.getBydevId(param.devId!!)
        if(door!=null){
            return Ref.error("该设备号已经存在")
        }
        door = Door()
        door.createTime = Date()
        door.deptId = param.deptId!!
        door.devId = param.devId!!
        door.doorAccessId = param.doorAccessId!!
        door.name = param.name!!
        door.lng = param.lng!!
        door.lat = param.lat!!
        door.accessOrderNum = param.accessOrderNum

        doorMapper.insert(door)
        return Ref.ok()
    }

    /**修改*/
    override fun modify(param: DoorModifyParam): CommonResult<Void>{

        var door: Door? = doorMapper.selectById(param.doorId)
        door?:return Ref.error("不存在的门id")
        door.deptId = param.deptId!!
        door.devId = param.devId!!
        door.doorAccessId = param.doorAccessId!!
        door.name = param.name!!
        door.lng = param.lng!!
        door.lat = param.lat!!
        door.accessOrderNum = param.accessOrderNum
        doorMapper.updateById(door)
        return Ref.ok()
    }

    /**删除*/
    override fun toDel(param: DoorToDelParam): CommonResult<Void>{

        var door: Door? = doorMapper.selectById(param.doorId)
        door?:return Ref.error("不存在的门id")
        var count = cardTimeBindMapper.countListByDoorId(param.doorId!!)
        if(count>0){
            return Ref.error("该门绑定了门卡,不能删除")
        }
        doorMapper.deleteById(param.doorId)
        return Ref.ok()
    }

    /**获取本部门所有的门*/
    override fun listAll(): CommonResult<List<DoorCardListDoorVo>>{

        val info = SystemUtil.getInfo()
        val list = doorMapper.listAllByDeptId(info.deptId)
        var vos = list.map {
            var vo = DoorCardListDoorVo()
            vo.doorId = it.id
            vo.doorName = it.name
            vo.doorDevId = it.devId
            vo
        }
        return Ref.ok(vos)
    }

    /**根据门禁id获取门禁分页列表*/
    override fun listByDoorAccessId(param: DoorListByDoorAccessIdParam)
            : CommonResult<DataGrid<DoorListByDoorAccessIdVo>>{

        var list = doorMapper.listByAccessId(param.doorAccessId!!,
                param.start!!,
                param.num!!)
        var count = doorMapper.countListByAccessId(param.doorAccessId!!)

        var vos = list.map {
            var vo = DoorListByDoorAccessIdVo()
            vo.doorId = it.id
            vo.createTime = TimeUtil.formater(it.createTime)
            vo.devId = it.devId
            vo.name = it.name
            vo.lng = it.lng
            vo.lat = it.lat
            vo.deptId = it.deptId
            var dept:SysDept? = deptMapper.selectById(it.deptId)
            vo.deptName = dept?.shortName?:""
            vo
        }
        var dataGrid = DataGrid<DoorListByDoorAccessIdVo>()
        dataGrid.rows = vos
        dataGrid.total = count
        return Ref.ok(dataGrid)
    }

    /**获取本部门和下属部门的所有门*/
    override fun listDoorContainChildren()
            : CommonResult<List<DoorListDoorContainChildrenVo>>{

        val info = SystemUtil.getInfo()
        var list = doorMapper.listByDeptIdList(info.deptIdList)
        var vos = list.map {
            var vo = DoorListDoorContainChildrenVo()
            vo.doorId = it.id
            vo.createTime = TimeUtil.formater(it.createTime)
            vo.devId = it.devId
            vo.name = it.name
            vo.lng = it.lng
            vo.lat = it.lat
            vo.deptId = it.deptId
            var dept:SysDept? = deptMapper.selectById(it.deptId)
            vo.deptName = dept?.shortName?:""
            var doorAccess:DoorAccess? = doorAccessMapper.selectById(it.doorAccessId)
            vo.accessId = it.doorAccessId
            vo.accessName = doorAccess?.name?:""
            vo
        }
        return Ref.ok(vos)
    }

    /**开门*/
    override fun toOpen(param: DoorToOpenParam): CommonResult<Void>{
        val door: Door? = doorMapper.selectById(param.doorId)
        door?:return Ref.error("错误的门id")
        return doorDriver.openDoor(door.doorAccessId,door.accessOrderNum)
    }

    /**关门*/
    override fun toClose(param: DoorToCloseParam): CommonResult<Void>{
        val door: Door? = doorMapper.selectById(param.doorId)
        door?:return Ref.error("错误的门id")
        return doorDriver.closeDoor(door.doorAccessId,door.accessOrderNum)
    }
}